from PIL import Image


def replace_non_white_to_green(image_path, output_path):
    # 打开图片
    image = Image.open(image_path)
    # 转换为RGBA模式以便处理透明通道
    image = image.convert("RGBA")
    # 获取图片数据
    data = image.getdata()

    new_data = []
    for item in data:
        # 保留色
        if item[0] == 0 and item[1] == 0 and item[2] == 0:
            new_data.append(item)
        else:
            # 替换色
            new_data.append((110, 206, 126, item[3]))  # 保留原始透明度

    # 更新图片数据
    image.putdata(new_data)
    # 保存处理后的图片
    image.save(output_path, "PNG")


# 使用示例
# image_path = r"D:\pic_video\s.png"
# output_path = r"D:\pic_video\s-green.png"
# image_path = r"D:\pic_video\s-green.png"
# output_path = r"D:\pic_video\s-2.png"
# image_path = r"D:\pic_video\s-2.png"
# output_path = r"D:\pic_video\s-3.png"
image_path = r"D:\pic_video\s-3.png"
output_path = r"D:\pic_video\s-4.png"
replace_non_white_to_green(image_path, output_path)
